import React, { Component } from 'react'
import * as dom from '../utilities/dom'
import * as color from '../configs/color'
import * as ENV from '../configs/env'
import logger from '../utilities/logger'
import DialogControl from '../components/Dialog/actions'

const titlePostfix = ENV.MODE == 'PE' ? '' : ('(@BUILD ' + ENV.BUILD + ')')

const defaultPageProps = {
    title: '叮咚小区',
    background: color.BGGRAY,
    className: ''
}

export default class Page extends Component {
    constructor(props) {
        super(props)
        const pageProps = Object.assign({}, defaultPageProps, this.props);

        // set page wrapper classname for css scope
        let className = ''
        if (!pageProps.className && props.routes.length > 1) {
            className += 'module-' + props.routes[1].path
        } else {
            className = 'page-' + props.className
        }
        dom.setBodyClassName(className)

        // set page title and background-color
        dom.setTitle(pageProps.title + titlePostfix)
        dom.setBodyBgColor(pageProps.background)

        // log page visit
        logger.collect({
            cid: props.routes[1].path,
            aid: 'visit',
            label: pageProps.title
        })

        this.alert = DialogControl.alert
        this.confirm = DialogControl.confirm
        this.notify = DialogControl.notify
        this.logger = logger
    }
}